<?php

/**
 * Controller for treePanel element
 * Return lists of databases and tables
 */
class TreeController extends Zend_Controller_Action
{

    /**
     * Disable layout
     */
    public function init()
    {

        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->getHelper('layout')->disableLayout();
        
    }
    

    /**
     * Forming Tree Panel with databases, tables and columns
     */
    public function indexAction()
    {
        $postParamNode = $this->_getParam('node');

        /**
         * If get param 'root' return list of data bases
         */
        if (strstr($postParamNode, 'root_'))
        {

            $schemataModel = new Default_Model_Schemata();
            $dataBases = $schemataModel->getAllDataBases();

            $formatetDataBases = array(); // initialize return array

            foreach ($dataBases as $base)
            {
                $formatetDataBases[] = array(
                    'text'           => $base['Database'],
                    'id'             => 'database_' . $base['Database'],
                    'iconCls'       => 'database',
                    'draggable'     => 'false'
                );
            }

            echo Zend_Json_Encoder::encode($formatetDataBases);

        } /**
         * If get param 'database' return list of tables from this data base
         */
        elseif(strstr($postParamNode, 'database_'))
        {

            $dataBaseName = substr($postParamNode, strlen('database_'));
            $tablesModel = new Default_Model_Tables();
            $tables = $tablesModel->getAllTablesFromDataBase($dataBaseName);

            $formatetTables = array(); // initialize return array

            foreach ($tables as $table)
            {
                $formatetTables[] = array(
                    'text'           => $table['Tables_in_' . $dataBaseName],
                    'id'             => 'table_' . $table['Tables_in_' . $dataBaseName],
                    'iconCls'       => 'databaseTable',
                    'draggable'     => 'false'
                );
            }

            echo Zend_Json_Encoder::encode($formatetTables);
            
        } /**
         * If get param 'table' return list of columns from this table
         */
        elseif (strstr($postParamNode, 'table_'))
        {

            $tableName = substr($postParamNode, strlen('table_'));
            $columnModel = new Default_Model_Columns();
            $columns = $columnModel->getAllColumnsFromTable($tableName);

            $formatetColumns = array(); // initialize array for return

            foreach ($columns as $column)
            {
                // if exist CHARACTER_MAXIMUM_LENGTH, then set it into brackets
                if ($column['CHARACTER_MAXIMUM_LENGTH'])
                    $column['CHARACTER_MAXIMUM_LENGTH'] = '(' . $column['CHARACTER_MAXIMUM_LENGTH'] . ')';

                // if exist EXTRA column, then separate it by comma
                if ($column['EXTRA'])
                    $column['EXTRA'] = ', ' .$column['EXTRA'];

                // for each COLUMN_KEY type set up icon class for css
                if ($column['COLUMN_KEY'] == 'PRI')
                    $iconClass = 'key';
                elseif ($column['COLUMN_KEY'] == 'UNI')
                    $iconClass = 'uni';
                elseif ($column['COLUMN_KEY'] == 'MUL')
                    $iconClass = 'mul';
                else
                    $iconClass = 'column';

                // write all properties into return array
                $formatetColumns[] = array(
                    'text'           => $column['COLUMN_NAME']
                                         . ' <span class="gr">'
                                         . $column['DATA_TYPE']
                                         . $column['CHARACTER_MAXIMUM_LENGTH']
                                         . $column['EXTRA']
                                         . '</span>',
                    'id'             => $column['COLUMN_NAME'],
                    'iconCls'       => $iconClass,
                    'draggable'     => false,
                    'leaf'           => true
                );
            }

            echo Zend_Json_Encoder::encode($formatetColumns);
            
        }
        
    }


}

